package me.jessyan.mvparms.demo.app.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import java.util.UUID;

/**
 * Created by 95 on 2017/8/28.
 */

public class Utils {
    //获取token
    public static String judgeToken(Context context) {
        if (Token.token == "") {
            SharedPreferences sharedPreferences = context.getSharedPreferences("UserParam", context.MODE_PRIVATE);
            Token.token = sharedPreferences.getString("token", "");
        }
        return Token.token;
    }

    public static String judgePhoneNumber(Context context) {
        if (Token.userPhone == "") {
            SharedPreferences sharedPreferences = context.getSharedPreferences("UserParam", context.MODE_PRIVATE);
            Token.userPhone = sharedPreferences.getString("phone", "");
        }
        return Token.userPhone;
    }
    //获取用户ID
    public static int judgeUserId(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("UserParam", context.MODE_PRIVATE);
        int userId = sharedPreferences.getInt("user_id", -1);
        return userId;
    }
    //获取uniqueID
    public static String getUniqueID() {
        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            serial = Build.class.getField("SERIAL").get(null).toString();
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }



    //获取登录状态
    public static  Boolean judgeLogin(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("UserParam", context.MODE_PRIVATE);
        String token = sharedPreferences.getString("token", "");
        if (token != "" && !token.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
}